/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;

        if(root == NULL)
            return res;

        queue<TreeNode*> cnt_layer;
        cnt_layer.push(root);
        queue<TreeNode*> child_layer;
        

        int num = 0;
        while(!cnt_layer.empty())
        {
            vector<int> cnt_res;
            int size = cnt_layer.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* cnt_root = cnt_layer.front();
                if(num%2 == 1)
                    cnt_res.insert(cnt_res.begin(), cnt_root->val);
                else
                    cnt_res.push_back(cnt_root->val);
                if(cnt_root->left)
                    cnt_layer.push(cnt_root->left);
                if(cnt_root->right)
                    cnt_layer.push(cnt_root->right);
                cnt_layer.pop();
            }
            res.push_back(cnt_res);
            num += 1;
        }

        return res;
    }
};